<?php  
namespace app\home\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Cookie;
class User extends Controller
{
	// 用户注册
    public function register()
    {
        return view('register');
    }
    // 用户注册操作
    public function register_add()
    {
//        $time = time();
//        $usersql = "select * from man_user";
//        $userinfo = Db::query($usersql);
//        $data = input('post.');
//        if(!captcha_check($data['code'])) {
//            // 校验失败
//            $this->error('验证码不正确');
//        }else{
//            if ($userinfo[0]['u_name']==$data['u_name']){
//                $this->error('该用户名已存在');
//            }else{
//                $sql = "insert into man_user values ('','','".$data['u_name']."','".$data['u_pwd']."','','$time','','','')";
//                $res = Db::execute($sql);
//                if ($res){
//                    $this->success('注册成功');
//                }
//            }
//        }
    }
    // 登录
    public function login()
    {
        return view('login');
    }
    // 登录操作
    public function loginOn()
    {
        $data = input('post.');
        $usersql = "select * from man_user where u_name = '".$data['u_name']."'";
        $userinfo = Db::query($usersql);
        if(!captcha_check($data['code'])) {
            // 校验失败
            $this->error('验证码不正确');
        }else{
            if ($data['u_pwd']==$userinfo[0]['u_pwd']){
                cookie('name',$userinfo[0]['u_name'],3600);//两小时后过期
                cookie('u_id',$userinfo[0]['u_id'],3600);
                $this->success('登录成功','index/index');
            }else{
                $this->error('密码错误');
            }
        }
    }
    // 登出
    public function loginOut()
    {
        Cookie::set('name',null);
        Cookie::set('u_id',null);
        return $this->redirect('user/login');
    }

    // 我的个人中心
    public function userindex()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $sql = "select * from man_product_sc where u_id=$u_id and sc_statc=1";
            $scData = Db::query($sql);

            $goodsData = array();
            foreach ($scData as $key => $value) {
                // 查询收藏的商品
                $sql = "select * from man_goods where g_id='$value[g_id]'";
                $goodData = Db::query($sql);
                $goodsData[] = $goodData[0];
            }
            $typeData = $this->alltype();
            return view('userindex',['typeData'=>$typeData,'scData'=>$scData,'goodsData'=>$goodsData]);
        }else{
            return redirect("user/login");
        }
    }
    // 我的个人资料
    public function usersub()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){

            $typeData = $this->alltype();
            return view('usersub',['typeData'=>$typeData]);
        }else{
            return redirect("user/login");
        }
    }
    // 我的收藏
    public function collect()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $sql = "select * from man_product_sc where u_id=$u_id and sc_statc=1";
            $scData = Db::query($sql);

            $goodsData = array();
            foreach ($scData as $key => $value) {
                // 查询收藏的商品
                $sql = "select * from man_goods where g_id='$value[g_id]'";
                $goodData = Db::query($sql);
                $goodsData[] = $goodData[0];
            }
            $typeData = $this->alltype();
            return view('collect',['typeData'=>$typeData,'scData'=>$scData,'goodsData'=>$goodsData]);
        }else{
            return redirect("user/login");
        }
        
    }


    // 申请退货
    public function usertui()
    {
        $sql="select * from man_order";
        $res=Db::query($sql);
        return view('usertui',['res'=>$res]);
           
    }
    //申请退货记录
    public function usertuiji(){

        return view('usertuiji');
    }

    // 收货地址
    public function newPath()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            // 收货地址
            $sql = "select * from man_address where u_id=$u_id";
            $addressData = Db::query($sql);

            // 省市区联动
            if($this->request->isAjax()){
                $area_id = input("get.area_id");
                $sql = "select * from man_area where area_parent_id=$area_id";
                $areaData = Db::query($sql);
                $return = array('areaData'=>$areaData);
                echo json_encode($return);
                die;
            }
            $sql = "select * from man_area where area_parent_id=0 and area_name='北京'";
            $areaData = Db::query($sql);

            $typeData = $this->alltype();
            return view('newPath',['typeData'=>$typeData,'areaData'=>$areaData,'addressData'=>$addressData]);
        }else{
            return redirect("user/login");
        }
        
    }
    // 设置默认
    public function newPathMr()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $ad_id = input("get.ad_id");
            $sql = "update man_address set ad_is_default=1 where u_id=$u_id and ad_id=$ad_id";
            Db::execute($sql);
            $sql = "update man_address set ad_is_default=0 where u_id=$u_id and ad_id!=$ad_id";
            Db::execute($sql);
            echo "1";
        }else{
            return redirect("user/login");
        }
    }
    // 收货地址添加
    public function newPathAdd()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $data = input("post.");
            if(!empty($data)){
                $sql = "select count(*) as num from man_address where u_id=$u_id";
                $area = Db::query($sql);
                if($area['0']['num']<5){
                    $str = '';
                    foreach ($data['country'] as $key => $value) {
                        $str .= ",".$value;
                    }
                    // 省市区id
                    $ad_address_id = substr($str,1);

                    $sql = "select area_name from man_area where area_id in($ad_address_id)";
                    $areaName = Db::query($sql);

                    $str = '';
                    foreach ($areaName as $key => $value) {
                        $str .= ",".$value['area_name'];
                    }
                    // 省市区+详情地址
                    $ad_address_xq = substr($str,1).','.$data['ad_address'];

                    $sql = "select * from man_address where u_id=$u_id and ad_name='$data[ad_name]' and ad_tel='$data[ad_tel]' and ad_address_id='$ad_address_id' and ad_address='$data[ad_address]' and ad_address_xq='$ad_address_xq' and ad_code='$data[ad_code]'";
                    $area = Db::query($sql);
                    if(!$area){
                        $sql = "insert into man_address values(null,$u_id,'$data[ad_name]','$data[ad_tel]','$ad_address_id','$data[ad_address]','$ad_address_xq','$data[ad_code]',0)";
                        $areaData = Db::query($sql);
                        echo "0";die;
                    }else{
                        echo "4";die;
                    }
                }else{
                    echo "3";die;
                }
            }else{
                echo "2";die;
            }
        }else{
            echo "1";die;
        }
    }

    // 收货地址修改
    public function newPathupd()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $ad_id = input("get.ad_id");
            // 收货地址
            $sql = "select * from man_address where u_id=$u_id and ad_id=$ad_id";
            $addressData = Db::query($sql);
            $addressInfo = $addressData[0];
            // 省市区联动
            if($this->request->isAjax()){
                $area_id = input("get.area_id");
                $sql = "select * from man_area where area_parent_id=$area_id";
                $areaData = Db::query($sql);
                $return = array('areaData'=>$areaData);
                echo json_encode($return);
                die;
            }
            $sql = "select * from man_area where area_parent_id=0 and area_name='北京'";
            $areaData = Db::query($sql);

            $typeData = $this->alltype();
            return view('newPathupd',['typeData'=>$typeData,'areaData'=>$areaData,'addressInfo'=>$addressInfo]);
        }else{
            return redirect("user/login");
        }
        
    }
    // 收货地址修改
    public function newPathUpdOne()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $data = input("post.");
            if(!empty($data)){
                $sql = "select count(*) as num from man_address where u_id=$u_id";
                $area = Db::query($sql);
                if($area['0']['num']<5){
                    $str = '';
                    foreach ($data['country'] as $key => $value) {
                        $str .= ",".$value;
                    }
                    // 省市区id
                    $ad_address_id = substr($str,1);

                    $sql = "select area_name from man_area where area_id in($ad_address_id)";
                    $areaName = Db::query($sql);

                    $str = '';
                    foreach ($areaName as $key => $value) {
                        $str .= ",".$value['area_name'];
                    }
                    // 省市区+详情地址
                    $ad_address_xq = substr($str,1).','.$data['ad_address'];

                    $sql = "select * from man_address where u_id=$u_id and ad_name='$data[ad_name]' and ad_tel='$data[ad_tel]' and ad_address_id='$ad_address_id' and ad_address='$data[ad_address]' and ad_address_xq='$ad_address_xq' and ad_code='$data[ad_code]'";
                    $area = Db::query($sql);
                    if(!$area){
                        $sql = "update man_address set ad_name='$data[ad_name]',ad_tel='$data[ad_tel]',ad_address_id='$ad_address_id',ad_address='$data[ad_address]',ad_address_xq='$ad_address_xq',ad_code='$data[ad_code]' where u_id=$u_id and ad_id='data[ad_id]'";
                        $areaData = Db::query($sql);
                        echo "0";die;
                    }else{
                        echo "4";die;
                    }
                }else{
                    echo "3";die;
                }
            }else{
                echo "2";die;
            }
        }else{
            echo "1";die;
        }
    }
    // 收货地址删除
    public function newPathDel()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $ad_id = input("get.ad_id");
            if(!empty($ad_id)){
                $sql = "delete from man_address where ad_id=$ad_id and u_id=$u_id";
                Db::execute($sql);
                echo "0";die;
            }else{
                echo "2";die;
            }
        }else{
            echo "1";die;
        }
    }


    // 分类查询
    public function alltype()
    {
        // 查询分类
        $sql = "select * from man_category";
        $typeData = Db::query($sql);
        $typeData = $this->createTreeBySon($typeData);
        return $typeData;
    }
//     public function tui(){
//         $sql = "select * from man_order";
//         $type=Db::query($sql);
//         return view("tui",['type'=>$type]);
//     }

    // 多级分类递归方法  多维数组
    public function createTreeBySon($info,$c_pid=0)
    {
        // 定义数组
        $newArr = array();
        // 处理
        foreach ($info as $k=>$v) {
            // 判断
            if($v['c_pid'] == $c_pid){

                // 把v赋给新数组追加一个下标
                $newArr[$k] = $v;

                // 调用自己 查找所有子辈放到son下
                $newArr[$k]['son'] = $this->createTreeBySon($info,$v['c_id']);
            }
        }
        // 数组返回
        return $newArr;
    }
}


?>